<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生主页</title>
    <script src="js/base.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }
        h1 {
            font-size: 2em;
            color: #333;
            text-align: center;
            margin: 0;
        }
        h4 {
            font-size: 1.2em;
            color: #555;
            text-align: center;
            margin: 20px 0;
        }
        #sname {
            color: #FF5722;
            font-weight: bold;
        }
        table {
            width: 100%;
            max-width: 800px;
            margin: 20px auto;
            border-collapse: collapse;
            text-align: center;
        }
        th, td {
            padding: 10px;
            border: 1px solid #ddd;
        }
        th {
            background-color: #007BFF;
            color: white;
        }
        td a {
            color: #007BFF;
            text-decoration: none;
            font-weight: bold;
        }
        td a:hover {
            text-decoration: underline;
        }
        .button-container {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin: 20px 0;
        }
        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        #buttons {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<h1>欢迎<span id="sname"></span>登录</h1>
<script>
    document.getElementById("sname").innerHTML=localStorage.getItem("sname");

</script>
<h4>学生选课</h4>
<div class="button-container"><button onclick="goManagerCourse()">管理已选课程</button></div>
<script>
    function goManagerCourse(){
        window.location.href="studentManagerCourse.html";
    }
</script>
<table id="courseData"border="1px">
    <tr>
        <td>课程号</td>
        <td>课程名称</td>
        <td>授课教师</td>
        <td>教室容量</td>
        <td>当前选课人数</td>
        <td colspan="3" align="center">操作</td>
    </tr>

</table>
<div id="buttons" style="position: relative;left: 200px;top:10px">

</div>
<script>
    let studentId=localStorage.getItem("sid");
    let pageNo=1;
    let pageSize=5;
    let pageCount=0;
    let arr=null;
    function loadData(){
        let url="http://localhost:8080/student/page?pageNo="+pageNo+"&pageSize="+pageSize+"&studentId="+studentId+"&state=0";
        $.rest(url,"get",null,function (r){
            if (r.code==200){
                arr=r.data.data;
                pageCount=r.data.pageCount;
                let courseDataInnerHTML="<tr>\n" +
                    "            <td>课程号</td>\n" +
                    "            <td>课程名称</td>\n" +
                    "            <td>授课教师</td>\n" +
                    "            <td>教室容量</td>\n" +
                    "            <td>当前选课人数</td>\n" +
                    "            <td colspan=\"3\">操作</td>\n" +
                    "        </tr>";
                for(let i=0;i<arr.length;i++){
                    let item=arr[i];
                    courseDataInnerHTML=courseDataInnerHTML+"<tr>" +
                        "<td>"+item.id+"</td>" +
                        "<td>"+item.name+"</td>" +
                        "<td>"+item.tname+"</td>" +
                        "<td>"+item.maxLimit+"</td>" +
                        "<td>"+item.count+"</td>" +
                        "<td colspan='3'><a href='#' onclick='return onSelectedClick("+(i)+")'>选择这门课程</a></td>" +
                        "</tr>";
                }
                document.getElementById("courseData").innerHTML= courseDataInnerHTML;

                let buttonsInnerHTML="";
                if (pageCount>0){
                    buttonsInnerHTML="<button onclick='onPrevClick();'>上一页</button>";
                    for(let i=0;i < pageCount;i++){
                        buttonsInnerHTML=buttonsInnerHTML+"<button onclick='onPageClick("+(i+1)+")'>"+(i+1)+"</button>";
                    }

                    buttonsInnerHTML=buttonsInnerHTML+"<button onclick='onNextClick();'>下一页</button>";
                }
                document.getElementById("buttons").innerHTML=buttonsInnerHTML;

            }
        });
    }
    loadData();
    function onPageClick(pNo){
        pageNo=pNo;
        loadData();

    }
    function onPrevClick(){
        if (pageNo==1){
            return;
        }
        pageNo--;
        loadData();
    }
    function onNextClick(){
        if (pageNo==pageCount){
            return
        }
        pageNo++;
        loadData();
    }
    function onSelectedClick(index){
        let item=arr[index];
        let courseId=item.id;
        let url="http://localhost:8080/student/editCourseState?studentId="+studentId+"&courseId="+courseId+"&state=1";
        $.rest(url,"get",null,function (r){
            if (r.code==200){
                alert("选课成功");
            }else{
                alert("选课失败,每人至多选择五门课程");
            }
            loadData();
        });

    }
</script>
</body>
</html>